package com.yonyou.cyxdms.sysAuth.service.controller.manage.version;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.service.version.VersionService;
import com.yonyou.cyxdms.sysAuth.service.vo.appversion.AppVersionVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author maxingjian
 * @since 2019-11-25
 */
@Api(value = "版本", tags = {"版本"})
@RestController
@RequestMapping("/version")
public class VersionController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(VersionController.class);

    @Autowired
     private VersionService versionService;
    /**
     * 保存版本信息
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/11/25
     */
    @PostMapping("/save")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header"), @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string",
            paramType = "header")})
    public String insertAppVersion(@RequestBody @Valid AppVersionVO appVersionVO) throws Exception {
        if(StringUtils.isNullOrEmpty(appVersionVO.getVersionId())) {
            versionService.insertAppVersion(appVersionVO);
        }else
        {
            versionService.updateAppVersion(appVersionVO);
        }
        return "";
    }

    /**
     * 查询
     *
     * @auther: maxingjian
     * @since: 2019/11/25
     */
    @ApiOperation(value="查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"), @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string",
            paramType = "header")
    })
    @GetMapping("/queryAppVersion")
    public IPage<AppVersionVO> queryAppVersion(@ApiParam(name = "pageNum", value = "当前页") @RequestParam("pageNum") Long current
            , @ApiParam(name = "limit", value = "分页大小") @RequestParam("limit") Long size) {
        Page<AppVersionVO> page = new Page(current, size);
        return versionService.queryAppVersion(page);
    }


    /**
     * 通过Id查询
     *
     * @auther: maxingjian
     * @since: 2019/11/25
     */
    @ApiOperation(value="通过Id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"), @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string",
            paramType = "header")
    })
    @GetMapping("/queryAppVersionById")
    public AppVersionVO queryAppVersionById(@ApiParam(name = "id", value = "版本id", required = false) @RequestParam(value = "id", required = false, defaultValue = "") String id) {
        return versionService.queryAppVersionById(id);
    }

    /**
     * 获取各app当前数据
     *
     * @auther: maxingjian
     * @since: 2019/11/25
     */
    @ApiOperation(value="获取各app当前数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header"), @ApiImplicitParam(value = "appId", name = "appId", required = true, dataType = "string",
            paramType = "header")
    })
    @GetMapping("/queryAppVersionByApp")
    public List<AppVersionVO> queryAppVersionByApp() {
        return versionService.queryAppVersionByApp();
    }


    /**
     * 根据安卓、ios类型获取当前数据
     *
     * @auther: maxingjian
     * @since: 2019/12/20
     */
    @ApiOperation(value="根据安卓、ios类型获取当前数据")
    @GetMapping("/queryDataByAppType")
    public AppVersionVO queryDataByAppType(@ApiParam(name = "appDownLoadType", value = "平台类型", required = true) @RequestParam(value = "appDownLoadType", defaultValue = "") String appDownLoadType,
                                           @ApiParam(name = "appId", value = "appId", required = true) @RequestParam(value = "appId", defaultValue = "") String appId)
    {
        return versionService.queryDataByAppType(appDownLoadType,appId);
    }
}